package dao.entities.control;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Range;

import utils.SessionUtils;
import views.annotations.View;
import dao.interfaces.IBase;

@Entity
@Table(schema = "GENERAL", name = "LORO")
@View.Views({ @View(name = "table", fields = "tamanyo,id,description"),
		@View(name = "record", fields = ""),
		@View(name = "selector", fields = "id,description")})
public class Loro implements IBase {

	@Column(unique = true)
	@NotEmpty
	private String description;
	@Range(min = 20, max = 40)
	private int tamanyo;

	@NotNull
	@ManyToOne
	private Color color;
	
	@OneToMany(fetch=FetchType.LAZY)
	private List<Color> colors;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long id;
	private String notes;
	private String user;
	private Date date;

	@PreUpdate
	@PrePersist
	private void updateSave() {
		date = new Date();
		user = (String) SessionUtils.getSessionAttribute("user");
	}

	@Override
	public Object getId() {
		return id;
	}

	@Override
	public void setId(Object id) {
		this.id = (long) id;
	};

	@Override
	public String getDescription() {
		return description;
	};

	@Override
	public void setDescription(String description) {
		this.description = description;
	}

	@Override
	public String getNotes() {
		return notes;
	};

	@Override
	public void setNotes(String notes) {
		this.notes = notes;
	}

	@Override
	public String getUser() {
		return user;
	};

	@Override
	public void setUser(String user) {
		this.user = user;
	}

	@Override
	public Date getDate() {
		return date;
	}

	@Override
	public void setDate(Date date) {
		this.date = date;
	}

	public Loro() {
		super();
	}

	public Loro(String description, String notes, String user, Date date) {
		super();
		this.description = description;
		this.notes = notes;
		this.user = user;
		this.date = date;
	}

	public int getTamanyo() {
		return tamanyo;
	}

	public void setTamanyo(int tamanyo) {
		this.tamanyo = tamanyo;
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	@Override
	public String toString() {
		return description;
	}

	public List<Color> getColors() {
		return colors;
	}

	public void setColors(List<Color> colors) {
		this.colors = colors;
	}

}
